package chapter1;


import java.io.*;
import java.util.*;

class Main_Interpeter implements Runnable{
    static String ReadLn(int maxLength){  
        byte line[] = new byte [maxLength];
        int length = 0;
        int input = -1;
        try{
            while (length < maxLength){
                input = System.in.read();
                if ((input < 0) || (input == '\n')) break; 
                line [length++] += input;
            }

            if ((input < 0) && (length == 0)) return null;  
            return new String(line, 0, length);
        }catch (IOException e){
            return null;
        }
    }

    public static void main(String args[])  
    {
        Main_Interpeter myWork = new Main_Interpeter();  
        myWork.run();            
    }

    public void run() {
        new myStuffs().run();
    }
}

class myStuffs implements Runnable{
    public void run()
    {
    	String input;
    	StringTokenizer idata;
    	String value;
    	int n,m, count;
    	String [] ram;
    	int [] register;
    		
    	input = Main.ReadLn(255);
    	
		idata = new StringTokenizer(input);

		n = Integer.parseInt(idata.nextToken());

		count = 0;

		while (count < n) 
		{
			m = 0;
			ram = new String[1000];
			register = new int[10];

			System.out.println();

			while ((input = Main.ReadLn(255)) != null) 
			{
				idata = new StringTokenizer(input);
			
				if (!input.equals("\r"))
				{
					value = idata.nextToken();
					
					ram[m] = value;
					m++;
				} else {
					break;
				}
			}

			System.out.println(interpreter(register, ram) + 1);

			count++;
		}
        
    }
    
    int interpreter(int [] registers, String [] ram)
    {
    	int instructionCount = 0;
		
		for (int index = 0; index < ram.length; index++)
		{
			String instruction = ram[index];
			int firstValue = Integer.parseInt(instruction.substring(0,1));
			int secondValue = Integer.parseInt(instruction.substring(1, 2));
			int thirdValue = Integer.parseInt(instruction.substring(2,3));
			int posEndValue = 0;
			
			switch(firstValue) 
			{
				case 1: // Halt
					if (secondValue == 0 && thirdValue == 0)
					{
						instructionCount++;
						posEndValue = index;
						break;
					}
				case 2: // Set register d to n.
					registers[secondValue] = thirdValue;
					instructionCount++;
					continue;
				case 3: // Add n to register d.
					registers[secondValue] += thirdValue;
					instructionCount++;
					continue;
				case 4: // Multiply register d by n.
					registers[secondValue] *= thirdValue;
					instructionCount++;
					continue;
				case 5: // Set register d to value of register s.
					registers[secondValue] = registers[thirdValue];
					instructionCount++;
					continue;
				case 6: // Add value of register s to register d.
					registers[secondValue] += registers[thirdValue];
					instructionCount++;
					continue;
				case 7: // Multiply register d by value of register s.
					registers[secondValue] *= registers[thirdValue];
					instructionCount++;
					continue;
				case 8: // set register d to the value in RAM whose address is in register a
					registers[secondValue] = Integer.parseInt(ram[thirdValue]);
					instructionCount++;
					continue;
				case 9: // set the value in RAM whose address is in register a to the value of register s
					ram[thirdValue] = "" + registers[secondValue];
					instructionCount++;
					continue;
				case 0: // goto the location in register d unless register s contains 0.
					if (thirdValue != 0)
					{
						instructionCount++;
					}
					continue;
				default:
					if (firstValue == 0 && secondValue == 0 && thirdValue == 0)
					{
						instructionCount++;
					}
			}
		}
		return instructionCount;
	}
}
	
